package com.youkeyi.ddy.cloud.basic.pub.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.user.entity.SysUserAuthLogin;
import com.youkeyi.ddy.cloud.basic.pub.user.service.SysUserAuthLoginService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.PublicUserAuthLoginCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserAuthLoginListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserAuthLoginListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserAuthLoginCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserAuthLoginQueryCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户小程序授权登陆记录表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-06-05
 */
@Service
public class SysUserAuthLoginController implements PublicUserAuthLoginCloudService {

    @Autowired
    private SysUserAuthLoginService sysUserAuthLoginService;

    @Override
    public PublicUserAuthLoginListCloudDto query(PublicUserAuthLoginQueryCloudParam param) {
        PublicUserAuthLoginListCloudDto dto = new PublicUserAuthLoginListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getUserAuthLoginPrimaryKeys())) {
            param.getUserAuthLoginPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getUserKeys())) {
            param.getUserKeys().removeAll(Collections.singleton(null));
        }


        QueryWrapper<SysUserAuthLogin> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getUserAuthLoginPrimaryKeys()), SysUserAuthLogin::getPrimaryKey, param.getUserAuthLoginPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getUserKeys()), SysUserAuthLogin::getUserKey, param.getUserKeys())
                .ne(SysUserAuthLogin::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysUserAuthLogin> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysUserAuthLogin> authLoginList = sysUserAuthLoginService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(authLoginList.getRecords())) {
            dto.setTotal(authLoginList.getTotal());

            List<PublicUserAuthLoginListItemCloudDto> items = new ArrayList<>();
            authLoginList.getRecords().forEach(authLogin -> {
                PublicUserAuthLoginListItemCloudDto item = new PublicUserAuthLoginListItemCloudDto();
                item.setUserAuthLoginPrimaryKey(authLogin.getPrimaryKey());
                item.setUserKey(authLogin.getUserKey());
                item.setUserName(authLogin.getUserName());
                item.setAuthCorpKey(authLogin.getAuthCorpKey());
                item.setAuthCorpCoordinate(authLogin.getAuthCorpCoordinate());
                item.setAuthAccount(authLogin.getAuthAccount());
                item.setAuthPhone(authLogin.getAuthPhone());
                item.setAuthTime(authLogin.getAuthTime() != null ? authLogin.getAuthTime().getTime() : null);
                item.setRemark(authLogin.getRemark());
                item.setIsDeleted(authLogin.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(authLogin.getCreateTime() != null ? authLogin.getCreateTime().getTime() : null);
                item.setCreateUserKey(authLogin.getCreateUserKey());
                item.setCreateUserName(authLogin.getCreateUserName());
                item.setUpdateTime(authLogin.getUpdateTime() != null ? authLogin.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(authLogin.getUpdateUserKey());
                item.setUpdateUserName(authLogin.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicUserAuthLoginCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getUserKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysUserAuthLogin user = new SysUserAuthLogin();
        user.setPrimaryKey(SimpleUUidUtils.getUuid());
        user.setUserKey(param.getUserKey());
        user.setUserName(param.getUserName());
        user.setAuthCorpKey(param.getAuthCorpKey());
        user.setAuthCorpCoordinate(param.getAuthCorpCoordinate());
        user.setAuthAccount(param.getAuthAccount());
        user.setAuthPhone(param.getAuthPhone());
        user.setAuthTime(param.getAuthTime() != null ? new Date(param.getAuthTime()) : null);
        user.setRemark(param.getRemark());
        user.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        user.setCreateUserKey(param.getCreateUserKey());
        user.setCreateUserName(param.getCreateUserName());
        user.setCreateTime(new Date());
        boolean save = sysUserAuthLoginService.save(user);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(user.getPrimaryKey());
        return rc;
    }
}
